package com.lyz.ehcachedome.service.impl;

import com.lyz.ehcachedome.entity.User;
import com.lyz.ehcachedome.service.EhcacheService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EhcacheServiceImpl implements EhcacheService {

    @Override
    @Cacheable(value = "cacheOne", key = "#id")
    public User findById(String id) {
        System.out.println("不走缓存查询");
        return new User("213102", "misleilei");
    }

    @CachePut(value = "cacheOne", key = "#user.userId")//key的生成：cacheOne::1
    public User insert(User user){
        System.out.println("加载数据入库");
        return user;
    }

    @CacheEvict(cacheNames = "cacheOne",key = "#id")//删除某个key对应的缓存数据
    public void deleteById(String id){
        System.out.println("cacheOne remove : "+ id);
    }
}
